
<template>
  <el-dialog v-model="dialogFormVisible" class="dialog-title" title="发送短信">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px" style="width: 400px; margin-left:50px;">
      <el-form-item label="电话号码" prop="phone">
        <el-input
          v-model="temp.phone"
          :autosize="{ minRows: 8, maxRows: 12}"
          type="textarea"
          placeholder="电话号码，多个以英文逗号“,”隔开"
        />
      </el-form-item>
      <el-form-item label="模版编码" prop="code">
        <el-input v-model="temp.code" placeholder="请设置模版编码" />
      </el-form-item>
      <el-form-item label="变量值" prop="data">
        <el-input ref="jsonEditor" v-model="temp.data" type="textarea"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button type="primary" @click="runSendMsg">发送短信</el-button>
    </template>
  </el-dialog>
</template>
<script>
import { sendMsg } from '@/api/sms/sendlog'

export default {
  emits: ['refreshList'],
  data() {
    return {
      rules: {
        phone: [{ required: true, message: '手机号码必填' }],
        code: [{ required: true, message: '编码必填' }]
      },
      dialogFormVisible: false,
      temp: {
        phone: '',
        code: '',
        data: JSON.parse('{}')
      }
    }
  },
  methods: {
    refreshList() {
      this.$emit('refreshList')
    },
    /**
     * 重置表单
     */
    resetTemp() {
      this.sendMsgLoading = false
      this.temp = {
        phone: '',
        code: '',
        data: JSON.parse('{}')
      }
    },
    /**
     * 显示发送短信对话框
     */
    handleSendMsg() {
      this.resetTemp()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 提交发送短信
     */
    runSendMsg() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.sendMsgLoading = true
          sendMsg(this.temp).then(response => {
            this.sendMsgLoading = false
            if (response.data.success) {
              this.dialogFormVisible = false
              this.refreshList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
